package com.mybatis.crudservice.controller;


import com.mybatis.crudservice.service.ReadService;
import com.mybatis.crudservice.tools.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ 描 述 : 公共controller - 支持跨域
 * @ 作 者 : 一念〃
 * @ 时 间 : 22:22 2019/5/21
 * @ 备 注 :
 */
@CrossOrigin
@RestController
@RequestMapping({"/read","/query","/service","/find"})
public class ReadController {

    @Autowired
    private ReadService service;

    /**
     * 查询单条
     * @param mapperName
     * @param selectName
     * @param params
     * @return
     */
    @RequestMapping(value={"/one/{mapperName}/{selectName}"}, method={RequestMethod.GET})
    public Object queryOne(@PathVariable String mapperName, @PathVariable String selectName, @RequestParam Map<String, Object> params) {
        //TODO 待处理from表单传递数组参数处理
        return service.readOne(mapperName, selectName, params);
    }

    /**
     * 查询全部 - 默认1000条
     * @param mapperName
     * @param selectName
     * @param params
     * @return
     */
    @RequestMapping(value={"/{mapperName}/{selectName}"}, method={RequestMethod.GET})
    public List<Map<String, Object>> queryAll(@PathVariable String mapperName, @PathVariable String selectName, @RequestParam Map<String, Object> params) {
        //TODO 待处理from表单传递数组参数处理
        return service.readAll(mapperName, selectName, params);
    }

    /**
     * 查询全部 - 分页查询
     * @param mapperName
     * @param selectName
     * @param params
     * @param page
     * @return
     */
    @RequestMapping(value={"/page/{mapperName}/{selectName}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
    public PageInfo<Map<String, Object>> queryPage(@PathVariable String mapperName, @PathVariable String selectName, @RequestParam Map<String, Object> params, PageInfo<Map<String, Object>> page) {
        //TODO 待处理from表单传递数组参数处理
        return service.readList(mapperName, selectName, params, page);
    }

}
